﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ProxyDelivery.App.Extenstions;
using Mengsk.ProxyDelivery.Service;
using Mengsk.ProxyDelivery.Domain;

namespace Mengsk.ProxyDelivery.App.UI.Sms
{
    /// <summary>
    /// Interaction logic for SmsHistoryUserControl.xaml
    /// </summary>
    public partial class SmsHistoryUserControl : UserControl, IPlugin
    {

        SmsHistoryService ss = ServiceContainer.GetService<SmsHistoryService>();

        public SmsHistoryUserControl()
        {
            InitializeComponent();
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            int targetId = 0;
            int.TryParse(this.tbTargetId.Text.Trim(), out targetId);
            this.pbBar.Parameters.Clear();
            this.pbBar.Parameters.Add("type", this.cbbTypes.GetSelectedEnum<Mengsk.ProxyDelivery.Domain.SmsHistoryType>());
            this.pbBar.Parameters.Add("targetId", targetId);
            this.pbBar.Parameters.Add("phone", this.tbPhone.Text.Trim());
            this.pbBar.Parameters.Add("startTime", this.dpStart.Value == null ? DateTime.MinValue : this.dpStart.Value.Value);
            this.pbBar.Parameters.Add("endTime", this.dpEnd.Value == null ? DateTime.MinValue : this.dpEnd.Value.Value);
            this.pbBar.StartPage();
        }

        private void btnModifyTime_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var item = this.dgvItems.SelectedItem as SmsHistory;
                if (item == null)
                {
                    MessageBox.Show("请选中数据再编辑");
                    return;
                }
                new SmsHistoryModifyWindow { Id = item.Id }.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pbBar_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            var ret = this.ss.GetByAll(e.GetParameter<SmsHistoryType>("type"), 0, e.GetParameter<int>("targetId"), "", e.GetParameter<DateTime>("startTime"), e.GetParameter<DateTime>("endTime"), 0, e.CurrentPage - 1, e.PageSize);
            this.pbBar.Total = ret.Total;
            this.pbBar.CurrentCount = ret.Datas.Count;
            this.dgvItems.ItemsSource = ret.Datas;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbbTypes.Bind<Mengsk.ProxyDelivery.Domain.SmsHistoryType>();
        }

        public string Title
        {
            get { return "短信历史"; }
        }

        public Control UIControl
        {
            get { return this; }
        }
    }
}
